package com.taplinker.client;

import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.taplinker.client.core.TaplinkerApplication;
import com.taplinker.client.crm.domain.CrmDataCenter;
import com.taplinker.client.domain.MenuDomain;
import com.taplinker.client.im.activity.IMQueueActivity;
import com.taplinker.client.im.domain.ImDataCenter;
import com.taplinker.client.widget.DrawerGarment;

public class BaseActivity extends ActivityGroup implements ActivityListen {
	protected DrawerGarment mLeftNav;
	private View mNavPublic;
	private TextView mNavPublicCount;
	private View mNavPrivate;
	private TextView mNavPrivateCount;

	private PopupWindow mStatusPopwin;
	private View mNavUserInfoArea;
	protected boolean mInitFlag = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TaplinkerApplication.getInstance().addActivity(this);
		if (mLeftNav == null && !mInitFlag) {
			mLeftNav = new DrawerGarment(this, R.layout.left_nav);
			mInitFlag = false;
		} else {
			mInitFlag = true;
		}
		if (!mInitFlag) {
			findView();
			initNav();

			mInitFlag = true;
		}
	}

	private void findView() {
		mNavUserInfoArea = mLeftNav.findViewById(R.id.nav_userinfo_area);
		mNavPublic = mLeftNav.findViewById(R.id.nav_public);
		mNavPublicCount = (TextView) mLeftNav
				.findViewById(R.id.nav_public_count);
		mNavPrivate = mLeftNav.findViewById(R.id.nav_private);
		mNavPrivateCount = (TextView) mLeftNav
				.findViewById(R.id.nav_private_count);

	}

	private void initNav() {
		mNavPublic.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				closeMenu();
				if (!MenuDomain.isOpen(MenuDomain.MenuItems.PUBLIC)) {
					MenuDomain.open(MenuDomain.MenuItems.PUBLIC);
					Intent it = new Intent(BaseActivity.this,
							IMQueueActivity.class);
					ImDataCenter.getInstance().setPublicQueue(true);
					startActivity(it);
				}
			}
		});
		mNavPrivate.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				closeMenu();
				if (!MenuDomain.isOpen(MenuDomain.MenuItems.PRIVATE)) {
					MenuDomain.open(MenuDomain.MenuItems.PRIVATE);
					Intent it = new Intent(BaseActivity.this,
							IMQueueActivity.class);
					ImDataCenter.getInstance().setPublicQueue(false);
					startActivity(it);
				}
			}
		});

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			if (mLeftNav != null && mLeftNav.isDrawerOpened()) {
				closeMenu();
				return false;
			} else {
				openMenu();
				return false;
			}
		}
		if (keyCode == KeyEvent.KEYCODE_BACK && mLeftNav != null
				&& mLeftNav.isDrawerOpened()) {
			closeMenu();
			return false;
		}
		if (keyboardShowing()) {
			hideKeyboard();
			return false;
		}
		return super.onKeyDown(keyCode, event);

	}

	protected void exitDialog() {
		new AlertDialog.Builder(this).setMessage("是否退出程序？")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						TaplinkerApplication.getInstance().exit();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}).create().show();
	}

	@Override
	protected void onResume() {
		super.onResume();
		ImDataCenter.getInstance().bindListener(this);
		CrmDataCenter.getInstance().bindListener(this);
		View v = findViewById(R.id.title_left_bt);
		if (v != null) {
			v.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					areaOnTouchListener();
					openMenu();
				}
			});
		}
		View rootView = getWindow().getDecorView();
		if (rootView != null) {
			rootView.setOnTouchListener(new View.OnTouchListener() {

				@Override
				public boolean onTouch(View v, MotionEvent event) {
					areaOnTouchListener();
					return false;
				}
			});
		}
		handleMessage(ImDataCenter.MENU_REFRESH);
		refreshMenuSelected();
	}

	protected void areaOnTouchListener() {
		hideKeyboard();
	}

	protected void refreshMenuSelected() {
		mNavPublic.setEnabled(MenuDomain
				.isClickable(MenuDomain.MenuItems.PUBLIC));
		mNavPrivate.setEnabled(MenuDomain
				.isClickable(MenuDomain.MenuItems.PRIVATE));

		mNavPublic.setSelected(MenuDomain.isOpen(MenuDomain.MenuItems.PUBLIC));
		mNavPrivate
				.setSelected(MenuDomain.isOpen(MenuDomain.MenuItems.PRIVATE));

	}

	@Override
	protected void onPause() {
		ImDataCenter.getInstance().unbindListener(this);
		CrmDataCenter.getInstance().unbindListener(this);
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		TaplinkerApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}

	protected void openMenu() {
		if (mLeftNav != null && !mLeftNav.isDrawerOpened()) {
			mLeftNav.openDrawer();
		}
	}

	protected void closeMenu() {
		if (mLeftNav != null && mLeftNav.isDrawerOpened()) {
			mLeftNav.closeDrawer();
		}
	}

	protected void hideKeyboard() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null && getCurrentFocus() != null) {
			imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
					InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}

	protected void showKeyboard() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null && getCurrentFocus() != null) {
			imm.showSoftInputFromInputMethod(
					getCurrentFocus().getWindowToken(),
					InputMethodManager.SHOW_FORCED);
		}
	}

	protected boolean keyboardShowing() {
		WindowManager.LayoutParams params = getWindow().getAttributes();
		if (params.softInputMode == WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE) {
			return true;
		}
		return false;
	}

	@Override
	public void handleMessage(String type) {
		if (type.equals(ImDataCenter.MENU_REFRESH)) {
			if (ImDataCenter.getInstance().getPubNum() > 0) {
				mNavPublicCount.setText(String.valueOf(ImDataCenter
						.getInstance().getPubNum()));
				mNavPublicCount.setVisibility(View.VISIBLE);
			} else {
				mNavPublicCount.setVisibility(View.GONE);
			}
			if (ImDataCenter.getInstance().getPriNum() > 0) {
				mNavPrivateCount.setText(String.valueOf(ImDataCenter
						.getInstance().getPriNum()));
				mNavPrivateCount.setVisibility(View.VISIBLE);
			} else {
				mNavPrivateCount.setVisibility(View.GONE);
			}

		}

	}

}
